/*
 * @ProjectName: flutter_common
 * @Package:
 * @ClassName: first_page.dart
 * @Description: 启动页面
 * @User: Page.Su
 * @Date: 2020/8/21
 * @Time: 10:28
 * @UpdateDate: 2020/8/21 10:28
 */

import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_su/common/language_text.dart';
import 'package:flutter_su/common/app_color.dart';
import 'package:flutter_su/common/application.dart';
import 'package:flutter_su/common/fun.dart';
import 'package:flutter_su/common/show.dart';
import 'package:flutter_su/enum/language_type.dart';
import 'package:flutter_su/provider/language_provider.dart';
import 'package:flutter_su/router/Audit_router.dart';
import 'package:flutter_su/router/login_register_router.dart';
import 'package:flutter_su/services/language_api.dart';
import 'package:provider/provider.dart';

class FirstPage extends StatefulWidget {
  @override
  _FirstPageState createState() => _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
  LanguageProvider _languageProvider; // app语言的状态管理

  @override
  void initState() {
    super.initState();
  }

  @override
  void didChangeDependencies() {
    _languageProvider = Provider.of<LanguageProvider>(context, listen: false);
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColor.MAIN_COLOR_BLUE,
      body: _body(),
    );
  }

  Widget _body() {
    return Container(
      alignment: Alignment.center,
      child: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Align(
                alignment: Alignment.centerRight,
                child: Padding(
                  padding: EdgeInsets.only(
                    right: 20,
                  ),
                  child: _language(),
                )),
            _imageIcon(),
            _recommendCode(),
            _registerButton(),
            _loginButton(),
          ],
        ),
      ),
    );
  }

  // 图标
  Widget _imageIcon() {
    return Container(
      margin: EdgeInsets.only(top: SFunCommon.height(265)),
      width: SFunCommon.width(430),
      height: SFunCommon.height(198),
      child: Image.asset("file/images/login_register/image1@3x.png"),
    );
  }

  // 语言切换
  Widget _language() {
    var inkWell = InkWell(
      onTap: () async {
        var success = await LanguageApi()
            .settingLanguage(_languageProvider.getLanguageType);
        if (success) {
          _languageProvider.switchLanguageType();
          Show.showMsgByProviderSuccess();
        } else {
          Show.showMsgByProviderError();
        }
      },
      child: SizedBox(
        width: SFunCommon.width(69),
        height: SFunCommon.height(38),
        child: Container(
          alignment: Alignment.center,
          decoration: BoxDecoration(
              color: AppColor.MAIN_COLOR_CYAN,
              borderRadius: BorderRadius.circular(SFunCommon.width(8))),
          child: LanguageText.setLanguage,
        ),
      ),
    );

    var align = Align(
        alignment: Alignment.centerRight,
        child: Padding(
          padding: EdgeInsets.only(
            right: SFunCommon.width(47),
          ),
          child: inkWell,
        ));

    return align;
  }

  // 推荐
  Widget _recommendCode() {
    return GestureDetector(
      onTap: ()async{
        // 模拟
        var data = await Application.router.navigateTo(
          context,
          AuditRouter.AUDIT_PAGE,
          transition: TransitionType.cupertinoFullScreenDialog,
        );
      },
      child: Container(
        margin: EdgeInsets.only(
          top: SFunCommon.height(453),
        ),
        child: LanguageText.recommendedCodeText,
      ),
    );
  }

  // 注册按钮
  Widget _registerButton() {
    return Container(
      margin: EdgeInsets.only(
        top: SFunCommon.height(44),
      ),
      width: SFunCommon.width(569),
      height: SFunCommon.height(75),
      child: RaisedButton(
        color: AppColor.MAIN_COLOR_CYAN,
        onPressed: () {
          Application.router.navigateTo(
            context,
            LoginRegisterRouter.REGISTER,
            transition: TransitionType.cupertinoFullScreenDialog,
          );
        },
        child: LanguageText.registerText,
        textColor: AppColor.MAIN_COLOR_BLUE,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(SFunCommon.width(20))),
      ),
    );
  }

  // 登录按钮
  Widget _loginButton() {
    return GestureDetector(
      onTap: () {
        Application.router.navigateTo(
          context,
          LoginRegisterRouter.LOGIN_EMAIL,
          transition: TransitionType.inFromBottom,
        );
      },
      child: Container(
        margin: EdgeInsets.only(
          top: SFunCommon.height(32),
        ),
        padding: EdgeInsets.only(
          left: SFunCommon.width(20),
          right: SFunCommon.width(20),
        ),
        child: LanguageText.loginText,
      ),
    );
  }
}
